package org.bzjj.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.bzjj.log.MyLog;

public class Tools {
	
	public static void main(String[] args) {
		long t1=dateFormat("2018-06-08 18:45:40.0").getTime();
		long t2=new Date().getTime();
		long r=(t2-t1)/(1000*60);
		System.out.println(r);
	}

	public static void baoBiao(Connection connection, String query, HSSFSheet sheet) {
		ResultSet result = null;
		Statement stmt = null;
		try {
			stmt = connection.createStatement();
			result = stmt.executeQuery(query);
			int clmnCount = result.getMetaData().getColumnCount();
			//行计数
			int row=1;
			//多一行记录序号
			String[] values = new String[clmnCount+1];
			while (result.next()) {
				values[0]=row+"";
				for (int i = 0; i < clmnCount; i++) {
					values[i+1] = result.getString(i + 1);
				}
				insertRow(sheet, row, values);
				row++;
			}
		} catch (Exception e) {
			Logger logger = MyLog.getLog("Tools");
			logger.error("获取统计数据错误", e);
		}

		try {
			stmt.close();
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void insertRow(HSSFSheet sheet, int rowNum, String[] values) {
		HSSFRow row = sheet.createRow(rowNum);
		for (int i = 0; i < values.length; i++) {
			row.createCell(i).setCellValue(values[i]);
		}
	}

	public static void saveFile(HSSFWorkbook workBook, File file) {
		if (file != null) {
			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				// 设置活动工作表
				workBook.setActiveSheet(0);
				workBook.write(file);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	public static void doOutputExcel(String path, String name, HttpServletResponse response) {
		try {
			OutputStream o = response.getOutputStream();
			byte[] b = new byte[500];
			File fileLoad = new File(path + name);
			response.setHeader("Content-disposition",
					"attachment;filename=" + new String(name.getBytes(), "ISO-8859-1"));
			response.setContentType("application/msexcel");
			long fileLength = fileLoad.length();
			String length = String.valueOf(fileLength);
			response.setHeader("Content-length", length);
			FileInputStream in = new FileInputStream(fileLoad);
			int n = 0;
			while ((n = in.read(b)) != -1) {
				o.write(b, 0, n);
			}
			o.flush();
			in.close();
			o.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static String dateFormat(Date date) {
		DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		return format.format(date);
	}
	
	public static String dateFormat1(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	public static String dateSimPleFormat(Date date) {
		DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
		return format.format(date);
	}
	
	public static Date dateFormat(String str) {
		SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		try {
			return sDateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
	}

	public static Properties getProPerty() {
		Properties properties = new Properties();
		// 使用ClassLoader加载properties配置文件生成对应的输入流
		InputStream in = Tools.class.getClassLoader().getResourceAsStream("config.properties");
		// 使用properties对象加载输入流
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

	public static void saveSql(String pathname, String str) {

		if (str == null || str.equals("")) {
			return;
		}

		File file = new File(pathname);

		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				Logger logger = MyLog.getLog("Tools");
				logger.error(e.getMessage());
			}
		}

		try {
			FileWriter writer = new FileWriter(file);
			writer.write(str);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			Logger logger = MyLog.getLog("Tools");
			logger.error(e.getMessage());
		}

	}

	public static String getPid(String pid) {
		try {
			InputStream in = Runtime.getRuntime().exec("cmd /c TASKLIST /FI \"PID eq " + pid + "\"").getInputStream();
			java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");
			if (s.hasNext()) {
				Pattern pattern = Pattern.compile("javaw.exe\\s+(\\d+)");
				Matcher matcher = pattern.matcher(s.next());
				if (matcher.find()) {
					return matcher.group(1);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void killPid(String pid) {
		try {
			Runtime.getRuntime().exec("cmd /c TASKKILL /F /PID " + pid);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
